#!/usr/bin/env python

# (c) 2007-2011 Helsinki University of Technology and University of Helsinki
# Licensed under the MIT license; see LICENSE.txt for more information.

from worker import Worker

import sys

class Mapper (Worker):
  def __init__(self, session):
    Worker.__init__(self, session.getFatalEvent())
    self._session = session
    self._mappers = {}

  def process(self, doc):
    ctype = list(doc.getMetadata(RDF.type))[0]
    if ctype not in self._mappers:
      mapper = get_mapper(ctype)
      if mapper is None:
        return False
      self._mappers[ctype] = mapper(self._session)
    return self._mappers[ctype].process(doc)

    return False

  def finish(self):
    for mapper in self._mappers.values():
      mapper.finish()

# mapper plugin registry
_mappers = {}

def register_mapper(ctype, cl):
  _mappers[ctype] = cl;

def get_mapper(ctype):
  cl = _mappers.get(ctype, None)
  if not cl:
    print >>sys.stderr, "No mappers found for type", ctype
  return cl

# import the mappers so they can register themselves
from mapper import *
